<template>
  <div class="orders-manage" v-if="tableData">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="id" prop="id" align="center"> </el-table-column>
      <el-table-column label="订单编号" prop="oid" align="center">
      </el-table-column>
      <el-table-column label="图片" width="120" align="center">
        <template slot-scope="scope">
          <img
            :src="
              require(`../../../../item-server/router/components/commodity/${scope.row.pic}`)
            "
            alt=""
            width="100%"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="title" align="center">
      </el-table-column>
      <el-table-column label="品类" prop="type" align="center">
      </el-table-column>
      <el-table-column label="购买数量" prop="count" align="center">
      </el-table-column>
      <el-table-column label="总额" prop="price_total" align="center">
      </el-table-column>
      <el-table-column label="购买时间" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            moment(scope.row.order_time).format("YYYY-MM-DD HH:mm:ss")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.order_status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              handleEdit(scope.$index, scope.row);
              dialogVisible = true;
            "
            >查看订单</el-button
          >

          <!-- 查看订单用户详情 -->
          <el-dialog title="收货地址" :visible.sync="dialogVisible">
            <el-descriptions
              class="margin-top"
              title="订单详细信息"
              :column="3"
              border
            >
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  用户名
                </template>
                {{ user_name }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"></i>
                  手机号
                </template>
                {{ phone }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-location-outline"></i>
                  性别
                </template>
                {{ gender }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">邮箱</template>
                {{ email }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-office-building"></i>
                  联系地址
                </template>
                {{ addr }}
              </el-descriptions-item>
            </el-descriptions>
          </el-dialog>
          <el-button
            size="mini"
            type="danger"
            @click="handleOut(scope.$index, scope.row)"
            :disabled="scope.row.order_status == '运输中'"
            >发货</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      tableData: [],
      userData: [],
      phone: "",
      user_name: "",
      email: "",
      gender: "",
      addr: "",
      dialogVisible: false,
      oid: "",
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    handleEdit(row, index) {
      let url = `stroll/customer?user=${index.user}`;
      this.axios.get(url).then((res) => {
        this.userData = res.data;
        let { u_true_name, uphone, uemail, ugender, uaddr } = this.userData[0];
        this.phone = uphone;
        this.user_name = u_true_name;
        this.email = uemail;
        this.gender = ugender;
        this.addr = uaddr;
      });
    },
    orderOid(oid) {
      this.oid = oid;
      this.getData();
    },
    handleOut(index, row) {
      let url = `stroll/outders?id=${row.id}`;
      this.$confirm("确认要发货吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "发货成功!",
          });
          this.axios.get(url).then((res) => {
            if (res) {
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消发货",
          });
        });
    },
    getData() {
      let url = `stroll/orders?oid=${this.oid}&shopId=${sessionStorage.getItem(
        "shopId"
      )}`;
      this.axios.get(url).then((res) => {
        this.tableData = res.data;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>